log using log_merge_AMAR.log, replace
clear all
**Set working directory to location of files**
*cd "location"


*** AMAR merge with country-level dataset ***

use "AMAR.Phase1.FINAL.1 (original).dta" 

generate country_id = .
order country_id, after(COUNTRY)
replace country_id = 36 if COUNTRY=="Afghanistan"
replace country_id = 12 if COUNTRY=="Albania"
replace country_id = 103 if COUNTRY=="Algeria"
replace country_id = 104 if COUNTRY=="Angola"
replace country_id = 37 if COUNTRY=="Argentina"
replace country_id = 105 if COUNTRY=="Armenia"
replace country_id = 67 if COUNTRY=="Australia"
replace country_id = 144 if COUNTRY=="Austria"
replace country_id = 106 if COUNTRY=="Azerbaijan"
replace country_id = 146 if COUNTRY=="Bahrain"
replace country_id = 24 if COUNTRY=="Bangladesh"
replace country_id = 147 if COUNTRY=="Barbados"
replace country_id = 107 if COUNTRY=="Belarus"
replace country_id = 148 if COUNTRY=="Belgium"
replace country_id = 52 if COUNTRY=="Benin"
replace country_id = 53 if COUNTRY=="Bhutan"
replace country_id = 25 if COUNTRY=="Bolivia"
replace country_id = 150 if COUNTRY=="Bosnia and Herzegovina"
replace country_id = 150 if COUNTRY=="Bosnia"
replace country_id = 68 if COUNTRY=="Botswana"
replace country_id = 19 if COUNTRY=="Brazil"
replace country_id = 152 if COUNTRY=="Bulgaria"
replace country_id = 54 if COUNTRY=="Burkina Faso"
replace country_id = 10 if COUNTRY=="Myanmar"
replace country_id = 10 if COUNTRY=="Burma"
replace country_id = 69 if COUNTRY=="Burundi"
replace country_id = 55 if COUNTRY=="Cambodia"
replace country_id = 108 if COUNTRY=="Cameroon"
replace country_id = 66 if COUNTRY=="Canada"
replace country_id = 70 if COUNTRY=="Cape Verde"
replace country_id = 71 if COUNTRY=="Central African Republic"
replace country_id = 109 if COUNTRY=="Chad"
replace country_id = 72 if COUNTRY=="Chile"
replace country_id = 110 if COUNTRY=="China"
replace country_id = 15 if COUNTRY=="Colombia"
replace country_id = 153 if COUNTRY=="Comoros"
replace country_id = 112 if COUNTRY=="Republic of Congo"
replace country_id = 111 if COUNTRY=="Dem. Rep. of the Congo"
replace country_id = 111 if COUNTRY=="Democratic Republic of Congo"
replace country_id = 73 if COUNTRY=="Costa Rica"
replace country_id = 154 if COUNTRY=="Croatia"
replace country_id = 155 if COUNTRY=="Cuba"
replace country_id = 156 if COUNTRY=="Cyprus"
replace country_id = 157 if COUNTRY=="Czech Republic"
replace country_id = 158 if COUNTRY=="Denmark"
replace country_id = 113 if COUNTRY=="Djibouti"
replace country_id = 114 if COUNTRY=="Dominican Republic"
replace country_id = 75 if COUNTRY=="Ecuador"
replace country_id = 13 if COUNTRY=="Egypt"
replace country_id = 22 if COUNTRY=="El Salvador"
replace country_id = 160 if COUNTRY=="Equatorial Guinea"
replace country_id = 115 if COUNTRY=="Eritrea"
replace country_id = 161 if COUNTRY=="Estonia"
replace country_id = 38 if COUNTRY=="Ethiopia"
replace country_id = 162 if COUNTRY=="Fiji"
replace country_id = 163 if COUNTRY=="Finland"
replace country_id = 76 if COUNTRY=="France"
replace country_id = 116 if COUNTRY=="Gabon"
replace country_id = 118 if COUNTRY=="Georgia"
replace country_id = 77 if COUNTRY=="Germany"
replace country_id = 77 if COUNTRY=="West Germany"
replace country_id = 7 if COUNTRY=="Ghana"
replace country_id = 164 if COUNTRY=="Greece"
replace country_id = 78 if COUNTRY=="Guatemala"
replace country_id = 63 if COUNTRY=="Guinea"
replace country_id = 119 if COUNTRY=="Guinea Bissau"
replace country_id = 166 if COUNTRY=="Guyana"
replace country_id = 166 if COUNTRY=="Guyana (British Guiana)"
replace country_id = 26 if COUNTRY=="Haiti"
replace country_id = 27 if COUNTRY=="Honduras"
replace country_id = 167 if COUNTRY=="Hong Kong"
replace country_id = 210 if COUNTRY=="Hungary"
replace country_id = 168 if COUNTRY=="Iceland"
replace country_id = 39 if COUNTRY=="India"
replace country_id = 56 if COUNTRY=="Indonesia"
replace country_id = 79 if COUNTRY=="Iran"
replace country_id = 80 if COUNTRY=="Iraq"
replace country_id = 81 if COUNTRY=="Ireland"
replace country_id = 169 if COUNTRY=="Israel"
replace country_id = 82 if COUNTRY=="Italy"
replace country_id = 64 if COUNTRY=="Cote D'Ivoire"
replace country_id = 120 if COUNTRY=="Jamaica"
replace country_id = 9 if COUNTRY=="Japan"
replace country_id = 83 if COUNTRY=="Jordan"
replace country_id = 121 if COUNTRY=="Kazakhstan"
replace country_id = 40 if COUNTRY=="Kenya"
replace country_id = 43 if COUNTRY=="Kosovo"
replace country_id = 171 if COUNTRY=="Kuwait"
replace country_id = 122 if COUNTRY=="Kyrgyzstan"
replace country_id = 123 if COUNTRY=="Laos"
replace country_id = 84 if COUNTRY=="Latvia"
replace country_id = 44 if COUNTRY=="Lebanon"
replace country_id = 85 if COUNTRY=="Lesotho"
replace country_id = 86 if COUNTRY=="Liberia"
replace country_id = 124 if COUNTRY=="Libya"
replace country_id = 124 if COUNTRY=="Libya (Tripolitania, Cyrenaica, Fezzan)"
replace country_id = 173 if COUNTRY=="Lithuania"
replace country_id = 174 if COUNTRY=="Luxembourg"
replace country_id = 176 if COUNTRY=="Macedonia"
replace country_id = 125 if COUNTRY=="Madagascar"
replace country_id = 87 if COUNTRY=="Malawi"
replace country_id = 177 if COUNTRY=="Malaysia"
replace country_id = 88 if COUNTRY=="Maldives"
replace country_id = 28 if COUNTRY=="Mali"
replace country_id = 65 if COUNTRY=="Mauretania"
replace country_id = 65 if COUNTRY=="Mauritania"
replace country_id = 180 if COUNTRY=="Mauritius"
replace country_id = 3 if COUNTRY=="Mexico"
replace country_id = 126 if COUNTRY=="Moldova"
replace country_id = 89 if COUNTRY=="Mongolia"
replace country_id = 183 if COUNTRY=="Montenegro"
replace country_id = 90 if COUNTRY=="Morocco"
replace country_id = 57 if COUNTRY=="Mozambique"
replace country_id = 127 if COUNTRY=="Nambibia"
replace country_id = 58 if COUNTRY=="Nepal"
replace country_id = 91 if COUNTRY=="Netherlands"
replace country_id = 185 if COUNTRY=="New Zealand"
replace country_id = 59 if COUNTRY=="Nicaragua"
replace country_id = 60 if COUNTRY=="Niger"
replace country_id = 45 if COUNTRY=="Nigeria"
replace country_id = 186 if COUNTRY=="Norway"
replace country_id = 187 if COUNTRY=="Oman"
replace country_id = 29 if COUNTRY=="Pakistan"
replace country_id = 92 if COUNTRY=="Panama"
replace country_id = 93 if COUNTRY=="Papua New Guinea"
replace country_id = 189 if COUNTRY=="Paraguay"
replace country_id = 30 if COUNTRY=="Peru"
replace country_id = 46 if COUNTRY=="Philippines"
replace country_id = 17 if COUNTRY=="Poland"
replace country_id = 21 if COUNTRY=="Portugal"
replace country_id = 94 if COUNTRY=="Qatar"
replace country_id = 190 if COUNTRY=="Romania"
replace country_id = 11 if COUNTRY=="Russia"
replace country_id = 129 if COUNTRY=="Rwanda"
replace country_id = 196 if COUNTRY=="Sao Tome and Principe"
replace country_id = 197 if COUNTRY=="Saudi Arabia"
replace country_id = 31 if COUNTRY=="Senegal"
replace country_id = 198 if COUNTRY=="Serbia"
replace country_id = 198 if COUNTRY=="Serbia and Montenegro"
replace country_id = 199 if COUNTRY=="Seychelles"
replace country_id = 95 if COUNTRY=="Sierra Leone"
replace country_id = 200 if COUNTRY=="Singapore"
replace country_id = 201 if COUNTRY=="Slovakia"
replace country_id = 202 if COUNTRY=="Slovenia"
replace country_id = 130 if COUNTRY=="Somalia"
replace country_id = 8 if COUNTRY=="South Africa"
replace country_id = 96 if COUNTRY=="Spain"
replace country_id = 131 if COUNTRY=="Sri Lanka"
replace country_id = 33 if COUNTRY=="Sudan"
replace country_id = 4 if COUNTRY=="Suriname"
replace country_id = 132 if COUNTRY=="Swaziland"
replace country_id = 5 if COUNTRY=="Sweden"
replace country_id = 6 if COUNTRY=="Switzerland"
replace country_id = 97 if COUNTRY=="Syria"
replace country_id = 48 if COUNTRY=="Taiwan"
replace country_id = 133 if COUNTRY=="Tajikistan"
replace country_id = 47 if COUNTRY=="Tanzania"
replace country_id = 49 if COUNTRY=="Thailand"
replace country_id = 117 if COUNTRY=="The Gambia"
replace country_id = 74 if COUNTRY=="East Timor"
replace country_id = 74 if COUNTRY=="Timor Leste"
replace country_id = 74 if COUNTRY=="Timor-Leste"
replace country_id = 134 if COUNTRY=="Togo"
replace country_id = 135 if COUNTRY=="Trinidad and Tobago"
replace country_id = 135 if COUNTRY=="Trinidad"
replace country_id = 98 if COUNTRY=="Tunisia"
replace country_id = 99 if COUNTRY=="Turkey"
replace country_id = 136 if COUNTRY=="Turkmenistan"
replace country_id = 50 if COUNTRY=="Uganda"
replace country_id = 100 if COUNTRY=="Ukraine"
replace country_id = 207 if COUNTRY=="United Arab Emirates"
replace country_id = 101 if COUNTRY=="United Kingdom"
replace country_id = 101 if COUNTRY=="UK"
replace country_id = 20 if COUNTRY=="United States of America"
replace country_id = 102 if COUNTRY=="Uruguay"
replace country_id = 140 if COUNTRY=="Uzbekistan"
replace country_id = 206 if COUNTRY=="Vanuatu"
replace country_id = 51 if COUNTRY=="Venezuela"
replace country_id = 34 if COUNTRY=="Vietnam"
replace country_id = 14 if COUNTRY=="Yemen"
replace country_id = 61 if COUNTRY=="Zambia"
replace country_id = 62 if COUNTRY=="Zimbabwe"
replace country_id = 127 if COUNTRY=="Namibia"
replace country_id = 117 if COUNTRY=="Gambia"
replace country_id = 41 if COUNTRY=="North Korea"
replace country_id = 41 if COUNTRY=="N. Korea"
replace country_id = 42 if COUNTRY=="Korea, South"
replace country_id = 42 if COUNTRY=="S. Korea"
replace country_id = 203 if COUNTRY=="Solomon Islands"
replace country_id = 236 if COUNTRY=="Zanzibar"
replace country_id = 32 if COUNTRY=="South Sudan"
replace country_id = 23 if COUNTRY=="Yemen People's Republic"
replace country_id = 178 if COUNTRY=="Malta"
replace country_id = 137 if COUNTRY=="East Germany"
replace country_id = 198 if COUNTRY=="Federal Republic of Yugoslavia"
replace country_id = 198 if COUNTRY=="Socialist Federal Republic of Yugoslavia"
replace country_id = 35 if COUNTRY=="South Vietnam"

rename YEAR year

* drop irrelevant variables
keep AMAR_Group NUMCODE  country_id year ECGR ECDIS PROT ECGR REB ECGR REPVIOL ECGR REPNVIOL ECGR

*** Remove empty data points ***
drop if AMAR_Group==""
xtset NUMCODE year

*** Recode missing
recode ECDIS (-99=.)
recode ECGR (-99=.)
recode PROT (-99=.)
recode REPNVIOL (-99=.)
recode REPVIOL (-99=.)
recode REB (-99=.)

*** Merge with country-level variables ***
merge m:1 country_id year using  "Copy of Main dataset", keepusing(lexical_index latent_gdppc_mean_log)

save "AMAR dataset for analysis"

****
log close
translate log_merge_AMAR.log log_merge_AMAR.pdf